﻿using UnityEngine;
using System.Collections.Generic;
using MacGruber;

namespace PluginIdea
{
    public class DecalObject : MVRScript
    {

        private List<string> decalTypeStrs = new List<string> { "UnlitDecal", "MetalDecal", "SpecularDecal" };
        private GameObject decalCube;

        private JSONStorableStringChooser decalTypeChooser;

        private JSONStorableColor decalMainColor;
        private JSONStorableFloat jsonStorableGlossniness;
        private JSONStorableUrl specGlossTexJsonStorableUrl;
        private JSONStorableColor jsonStorableSpecularColor;
        private JSONStorableUrl jsonStorableSpecJsonStorableUrl;
        private JSONStorableFloat jsonStorableBumpScale;

        private UIDynamicButton createDecalBtn;
        private UIDynamicButton deleteDecalBtn;

        private JSONStorableUrl abedoJsonStorableUrl;
        private JSONStorableUrl normalJsonStorableUrl;

        public override void Init()
        {
            base.Init();

            if (containingAtom.type != "Empty")
            {
                SuperController.LogError("need add on Empty Atom");
                return;
            }

            string assetsBundlePath = Utils.GetPluginPath(this) + "/shaders.assetbundle";
            if (!EasyDecalManager.Instance.IsInitied) EasyDecalManager.Instance.Init(assetsBundlePath);

            CreateUI();

        }

        private void OnCreateDecalObject()
        {
            decalCube = null;
            decalCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            decalCube.transform.SetParent(containingAtom.reParentObject.Find("object/rescaleObject"));
            decalCube.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
            decalCube.transform.localPosition = Vector3.zero;
            decalCube.transform.localRotation = Quaternion.identity;
            containingAtom.transform.localRotation = Quaternion.Euler(new Vector3(90, 0, 0));

            string shaderName = string.Format("MyDecal/{0}", decalTypeChooser.val);
            SuperController.LogMessage(shaderName);
            var shader = EasyDecalManager.Instance.dic_BundleShaders[shaderName];
            SuperController.LogMessage(shader.name);
            var decalCubeMaterial = new Material(EasyDecalManager.Instance.dic_BundleShaders[shaderName]);
            decalCube.GetComponent<MeshRenderer>().material = decalCubeMaterial;

            var meshLineDrawer = decalCube.AddComponent<MeshLineDraw>();
        }

        private void CreateUI()
        {
            decalTypeChooser = Utils.SetupStringChooser(this, "Decal Type", decalTypeStrs, 0, false);
            decalTypeChooser.setCallbackFunction = (choose) => {

                if (decalCube != null)
                {
                    Shader shader = null;
                    if (choose == "UnlitDecal")
                    {
                        shader = EasyDecalManager.Instance.dic_BundleShaders["MyDecal/UnlitDecal"];
                    }
                    else if (choose == "MetalDecal")
                    {
                        shader = EasyDecalManager.Instance.dic_BundleShaders["MyDecal/MetalDecal"];
                    }
                    else if (choose == "SpecularDecal")
                    {
                        shader = EasyDecalManager.Instance.dic_BundleShaders["MyDecal/SpecularDecal"];
                    }

                    decalCube.GetComponent<MeshRenderer>().material.shader = shader;
                }
            };

            createDecalBtn = Utils.SetupButton(this, "Create", OnCreateDecalObject, false);

            createDecalBtn = Utils.SetupButton(this, "Clear", ()=> {
                if (decalCube != null)
                {
                    Destroy(decalCube);
                }
            }, false);

            Utils.SetupToggle(this, "Show Frame", true, true).setCallbackFunction = (v)=> {
                if (decalCube != null)
                {
                    decalCube.GetComponentInChildren<MeshLineDraw>().enabled = v;
                }
            };

            //MainColor
            decalMainColor = Utils.SetupColor(this, "MainColor", Color.white, false);
            decalMainColor.setCallbackFunction = (h, s, v) => {
                decalCube.GetComponent<MeshRenderer>().material.SetColor("_Color", Color.HSVToRGB(h, s, v));
            };

            //Abedo
            abedoJsonStorableUrl = Utils.SetupTexture2DChooser(this, "Set AbedoTex", $"Custom/Assets/MyAssets", true, new TextureSettings(), (tex) => {
                decalCube.GetComponent<MeshRenderer>().material.SetTexture("_MainTex", tex);
                float abdeoHWRaido = (float)tex.height / tex.width;
                decalCube.transform.localScale = new Vector3(decalCube.transform.localScale.x, decalCube.transform.localScale.x * abdeoHWRaido, decalCube.transform.localScale.z);
            });

            //Gloss
            jsonStorableGlossniness = Utils.SetupSliderFloatWithRange(this, "Gloss", 0f, 0f, 1f, false);
            jsonStorableGlossniness.setCallbackFunction = (val) => {
                decalCube.GetComponent<MeshRenderer>().material.SetFloat("_Glossiness", val);
            };

            //Spec
            specGlossTexJsonStorableUrl = Utils.SetupTexture2DChooser(this, "Set GlossTex", $"Custom/Assets/MyAssets", true, new TextureSettings(), (tex) => {
                decalCube.GetComponent<MeshRenderer>().material.SetTexture("_SpecGlossMap", tex);
            });

            jsonStorableSpecularColor = Utils.SetupColor(this, "SpecularColor", Color.black, true);
            jsonStorableSpecularColor.setCallbackFunction = (c, s, v) => {
                decalCube.GetComponent<MeshRenderer>().material.SetColor("_SpecColor", Color.HSVToRGB(c, s, v));
            };

            //bump
            normalJsonStorableUrl = Utils.SetupTexture2DChooser(this, "Set BumpMapTex", $"Custom/Assets/MyAssets", true, new TextureSettings(), (tex) => {
                decalCube.GetComponent<MeshRenderer>().material.SetTexture("_BumpMap", tex);
            });

            jsonStorableBumpScale = Utils.SetupSliderFloat(this, "BumpScale", 1f, 0.5f, 3f, true);
            jsonStorableBumpScale.setCallbackFunction = (val) => {
                decalCube.GetComponent<MeshRenderer>().material.SetFloat("_BumpScale", val);
            };

            //emission
            Utils.SetupColor(this, "EmissionColor", Color.black, true).setCallbackFunction = (h, s, v) => {
                decalCube.GetComponent<MeshRenderer>().material.SetColor("_EmissionColor", Color.HSVToRGB(h, s, v));
            };

        }

        private void OnDestroy()
        {
            if (decalCube != null)
            {
                Destroy(decalCube);
            }
        }
    }
}


